<!-- 营销管理-商品定价 -->
<template>
    <div>
        <el-card style="max-width: 100%;">
            <search-box :tableDataInput="tableData" @search="handleSearch"></search-box>
            <el-button style="position: absolute;left: 550px;" v-if="FixPriceButton" round
                @click="FixPriceList">批量审核</el-button>
            <span style="position: absolute;left: 670px;top: 112px;color: rgb(191, 202, 222);"
                v-if="FixPriceButton">已选：{{ FixPriceLength }}</span>
            <el-button class="btn1" @click="showFixPriceDialog"><i class="el-icon-plus"
                    style="margin-right: 3px;"></i>定价单</el-button>
            <el-table :data="selectedItems" style="font-size: 16px;position: relative;top:-20px;"
                :header-cell-style="{ background: '#EBEEF2' }" @selection-change="checkBox">
                <el-table-column type="selection" align="center" class="select" />
                <el-table-column prop="pic" label="商品图片" align="center" width="130px">
                    <template #default="scope">
                        <el-image style="width: 102px; height: 70px;border-radius: 10px;" :src="scope.row.pic?scope.row.pic:''"></el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="commodityName" label="商品名称" align="center">
                    <template slot-scope="scope">
                        <span @click="opendetails(scope.row)">{{ scope.row.commodityName }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="productSn" label="货号" align="center" />
                <el-table-column prop="spData" label="商品属性" width="250px" :show-overflow-tooltip="true" align="center">
                    <template slot-scope="scope">
                        <div>
                            <span v-for="(attr, index) in formatAttributes(scope.row.spData)" :key="index">
                                {{ attr }}
                                <span v-if="index < formatAttributes(scope.row.spData).length - 1">，</span>
                            </span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="commodityUnit" label="计量单位" align="center" />
                <el-table-column prop="stockNumber" label="库存数量" align="center" />
                <el-table-column prop="stockPrice" label="库存单价" align="center" />
                <el-table-column prop="fixPrice" label="零售单价" align="center">
                    <template #default="scope">
                        <div v-if="scope.row.fixPrice == 0">面议</div>
                        <div v-else>{{ scope.row.fixPrice }}</div>
                    </template>
                </el-table-column>
                <el-table-column prop="createTime" label="创建时间" align="center" />
                <el-table-column prop="state" label="状态" align="center">
                    <template #default="scope">
                        <span v-if="scope.row.state == 0" style="color: red;">待审核</span>
                        <span v-if="scope.row.state == 1">已同意</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <div @click="auditing(scope.$index, scope.row)" v-if="scope.row.state == 0">
                            <img src="../../assets/marketing/Auditing.svg" class="auditing">
                            <span style="margin-left: 5px;">审核</span>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 定价单的弹窗 -->
            <AddPricingList :dialogVisible.sync="FixPriceDialog" @reloadData="handleReloadData" />
            <ShowPriceDetail :dialogVisible.sync="FixPriceDetailDialog" :rowData="currentRowData"/>
        </el-card>
        <el-pagination
            background
            layout="->,prev, pager,next,total"
            :page-size="10"
            :total="tableDataLength"
            @current-change="handleCurrentChange"
            style="margin-top: 20px;">
        </el-pagination>
    </div>
</template>

<script>
import { getProductsPricelist,surePrice } from '@/api/api.js'
import AddPricingList from './components/AddPricingList.vue'
import ShowPriceDetail from './components/ShowPriceDetail.vue'
export default {
    components: {
        AddPricingList,
        ShowPriceDetail
    },
    data() {
        return {
            selectedItems: [],
            tableData: [],
            tableDataLength:0,
            input3: '',
            FixPriceDialog: false,
            FixPriceDetailDialog: false,
            currentRowData: {},
            FixPriceButton: false,
            FixPriceLength: '',
            commoditydeatil:{ //请求数据
                name:'',
                pageNo:1,
            }
        }
    },
    async created() {
        await this.getProductsPricelist(this.commoditydeatil)
    },
    methods: {
        async getProductsPricelist(data) {
            try {
                const res = await getProductsPricelist(data)
                console.log(res)
                if (res.data.code == 200) {
                    this.selectedItems = res.data.result.records;
                    this.tableData = res.data.result.records;
                    this.tableDataLength = res.data.result.total;
                }
            } catch (error) {
                console.log(error)
            }
        },
        formatAttributes(attributes) {
            let parsedAttributes = [];
            try {
                parsedAttributes = JSON.parse(attributes);
            } catch (e) {
                console.error('Error parsing attributes:', e);
            }

            if (Array.isArray(parsedAttributes)) {
                return parsedAttributes.map(attr => `${attr.key}:${attr.value}`);
            } else {
                return [];
            }
        },
        handleCurrentChange(val){
            this.commoditydeatil.pageNo = val
            this.getProductsPricelist(this.commoditydeatil)
            this.currentpageNumber = val
        },
        handleSearch(name) {
            this.commoditydeatil.name = name
            this.getProductsPricelist(this.commoditydeatil)
        },
        showFixPriceDialog() {
            this.FixPriceDialog = true;
        },
        opendetails(row) {
            console.log(row)
            console.log(row.batchNumber)
            this.currentRowData = row;
            this.FixPriceDetailDialog = true;
        },
        auditing(index, row) {
            this.$confirm('是否审核通过', '提示', {
                    confirmButtonText: '通过',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(()=>{
                    surePrice(row.id).then((res)=>{
                        if(res.data.code == 200){
                            this.$message({
                                message: '操作成功',
                                type: 'success'
                            })
                            row.state = 1
                        }
                    })
                }).catch(()=>{
                      
                })
        },
        checkBox(selection) {
            console.log(selection)
            if (selection.length >= 1) {
                this.FixPriceButton = true
                this.FixPriceLength = selection.length
            } else
                this.FixPriceButton = false
        },
        handleReloadData() {
            // 调用 getAllStocks 方法
            this.commoditydeatil.pageNo = this.currentpageNumber;
            this.getProductsPricelist(this.commoditydeatil);
        },
        FixPriceList() {
            this.$message({
                message: '点击批量审核',
                type: 'success'
            })
        }
    }
}
</script>

<style scoped>
.input1 {
    font-size: 16px;
    width: 241px;
    opacity: 1;
    border-radius: 80px;
    background: rgb(153, 155, 158);
}

.input1>>>input.el-input__inner {
    background: rgba(235, 238, 242, 1);
    border-radius: 20px;
}

.btn1 {
    position: relative;
    float: right;
    top:-40px;
    opacity: 1;
    border-radius: 80px;
    width: 130px;
    height: 38px;
    color: white;
    font-size: 15px;
    background: rgba(126, 114, 242, 1);
}


.auditing {
    width: 16px;
    height: 16px;
    position: relative;
    top: 2px;
}

.el-pagination {
    float: right;
}

</style>